package com.cenarius.game.tools.mapcatcher;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class MapCatcher {
	public static Integer WIDTH = 256;
	public static Integer HEIGHT = 256;
	
	public static Integer X_FROM=3155;
	public static Integer X_TO=3283;
	public static Integer Y_FROM=1633;
	public static Integer Y_TO=1740;

	public static Integer TMP_X_FROM=3227;
	public static Integer TMP_X_TO=3237;
	public static Integer TMP_Y_FROM=1676;
	public static Integer TMP_Y_TO=1686;
	
	/**
	 * @param args
	 * @throws Throwable
	 */
	public static void main(String[] args) throws Throwable {
		for(Integer x=TMP_X_FROM;x<=TMP_X_TO;x++){
			for(Integer y=TMP_Y_FROM;y<=TMP_Y_TO;y++){
				MapCatcher
				.download(
						"http://mt2.google.cn/vt/lyrs=t@128,r@175000000&hl=zh-CN&gl=cn&src=app&x="+x+"&y="+y+"&z=12&s=G",
						"D:/workspace/tgs/WebContent/map/86/51/"+x+","+y+".jpg");
			}
		}
		

	}

	public static void download(String urlName, String tofile) throws Throwable {
		FileOutputStream baos = new FileOutputStream(tofile);
		BufferedInputStream bis = null;
		HttpURLConnection urlconnection = null;
		URL url = null;
		byte[] buf = new byte[1024];
		try {
			url = new URL(urlName);
			urlconnection = (HttpURLConnection) url.openConnection();
			urlconnection.connect();
			bis = new BufferedInputStream(urlconnection.getInputStream());
			for (int len = 0; (len = bis.read(buf)) != -1;) {
				baos.write(buf, 0, len);
			}
			
			
		} finally {
			try {
				urlconnection.disconnect();
				bis.close();
			} catch (IOException ignore) {
			}
		}
	}

}
